package org.wdcode.base.struts.action;

import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.opensymphony.xwork2.ActionSupport;

import org.wdcode.base.util.Struts2Util;
import org.wdcode.common.constants.StringConstants;
import org.wdcode.common.tools.Conversion;
import org.wdcode.web.params.WdWebParams;
import org.wdcode.web.util.AttributeUtil;
import org.wdcode.web.util.CookieUtil;
import org.wdcode.web.util.RequestUtil;

/**
 * Struts2 Action 的抽象实现 其它Struts2 Action可继承此类
 * @author WD
 * @since JDK6
 * @version 1.0 2009-08-26
 */
public abstract class BaseAction extends ActionSupport {
	// 序列化ID
	private static final long		serialVersionUID	= 3314538887531859725L;

	/* 方法返回值常量 */
	/**
	 * 用于Action方法返回值 list
	 */
	protected final static String	LIST				= "list";
	/**
	 * 用于Action方法返回值 add
	 */
	protected final static String	ADD					= "add";
	/**
	 * 用于Action方法返回值 del
	 */
	protected final static String	DEL					= "del";
	/**
	 * 用于Action方法返回值 edit
	 */
	protected final static String	EDIT				= "edit";
	
	/**
	 * 获得程序路径
	 * @return 程序路径
	 */
	public final String getRealPath() {
		return getRealPath(StringConstants.BACKSLASH);
	}

	/**
	 * 获得程序路径
	 * @param path 路径
	 * @return 程序路径
	 */
	public final String getRealPath(String path) {
		return getServletContext().getRealPath(path);
	}

	/**
	 * 获得项目路径
	 * @return 项目路径
	 */
	public final String getPath() {
		return getRequest().getContextPath();
	}

	/**
	 * 获得提交IP
	 * @return 提交IP
	 */
	public final String getIP() {
		return RequestUtil.getIP(getRequest());
	}

	/**
	 * 获得用户ID
	 */
	public final int getUserId() {
		return Conversion.toInt(get(WdWebParams.getSaveUser()));
	}

	/**
	 * 设置用户ID
	 * @param id 用户ID
	 */
	public final void setUserId(int id) {
		set(WdWebParams.getSaveUser(), id);
	}

	/**
	 * 设置属性
	 * @param key 属性键
	 * @param value 属性值
	 */
	public final void set(String key, Object value) {
		AttributeUtil.set(getRequest(), getResponse(), key, value);
	}

	/**
	 * 根据属性键获得属性值
	 * @param key 属性键
	 * @return 属性值
	 */
	public final Object get(String key) {
		return AttributeUtil.get(getRequest(), key);
	}

	/**
	 * 删除属性
	 * @param key 属性键
	 */
	public final void remove(String key) {
		AttributeUtil.remove(getRequest(), getResponse(), key);
	}

	/**
	 * 获得Request
	 * @return Request
	 */
	protected final HttpServletRequest getRequest() {
		return Struts2Util.getRequest();
	}

	/**
	 * 获得Response
	 * @return Response
	 */
	protected final HttpServletResponse getResponse() {
		return Struts2Util.getResponse();
	}

	/**
	 * 获得ServletContext
	 * @return ServletContext
	 */
	protected final ServletContext getServletContext() {
		return Struts2Util.getServletContext();
	}

	/**
	 * 获得Session
	 * @return Session
	 */
	protected final HttpSession getSession() {
		return Struts2Util.getSession();
	}

	/**
	 * 获得Session
	 * @param b
	 * @return Session
	 */
	protected final HttpSession getSession(boolean b) {
		return Struts2Util.getSession(b);
	}

	/**
	 * 获得Struts2 提供的Session代替对象,是个Map
	 * @return Map<String, Object>
	 */
	protected final Map<String, Object> getSessionMap() {
		return Struts2Util.getSessionMap();
	}

	/**
	 * 添加错误信息 错误Field=key value=国际化value
	 * @param key 国际化文件的Key
	 */
	protected final void addFieldError(String key) {
		addFieldError(key, getText(key));
	}

	/**
	 * 添加信息 调用addActionMessage做国际化处理
	 * @param key 国际化文件的Key
	 */
	protected final String addMessage(String key) {
		addActionMessage(getText(key));
		return key;
	}

	/**
	 * 获得客户端的Cookie数组
	 * @return Cookie数组
	 */
	protected final Cookie[] getCookies() {
		return getRequest().getCookies();
	}

	/**
	 * 添加Cookie到客户端
	 * @param name CookieName
	 * @param value CookieValue
	 */
	protected final void addCookie(String name, String value) {
		CookieUtil.add(getResponse(), name, value);
	}

	/**
	 * 删除Cookie
	 * @param name CookieName
	 * @param value CookieValue
	 */
	protected final void removeCookie(String name) {
		CookieUtil.remove(getResponse(), name);
	}

	/**
	 * 根据name获得Cookie 没找到返回null
	 * @param name CookieName
	 * @return Cookie
	 */
	protected final Cookie getCookie(String name) {
		return CookieUtil.getCookie(getRequest(), name);
	}

	/**
	 * 根据name获得CookieValue 没找到返回""
	 * @param name CookieName
	 * @return CookieValue
	 */
	protected final String getCookieValue(String name) {
		return CookieUtil.getCookieValue(getRequest(), name);
	}
}
